package array;

import java.util.Arrays;
import java.util.Random;
/*需求
* 在ACM竞赛中，通过排序算法进行现场排名，排名规则：题目数量通过率高的排名靠前，多个队伍通过的题目数量相等是，用时最少的队伍排名靠前*/
public class ACM {
    public static void main(String[] args) {
    shout();
    }
    public static void shout(){
        Random random = new Random();
        /*名字存储*/
        String[] na = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x",
                "y","z"};
        /*创建一个二维数组*/
        String[][] array = new String[10][3];
        System.out.println("数组原样");
        for (int i = 0; i < array.length; i++) {
            /*
             * @param name 队伍名字
             * @param number 答题数量
             * @param time 答题时间*/
            int number = random.nextInt(21);/*随机生成答完的题目数量，最多20*/
            int time = random.nextInt(91);/*随机生成答题时间，最多90分钟*/
            int name = random.nextInt(na.length);/*随机获取名字*/
            array[i][0] = na[name];/*将名字赋给数组*/
            array[i][1] = String.valueOf(number);/*将题目数量赋给数组*/
            array[i][2] = String.valueOf(time);/*将答题时间赋给数组*/
            System.out.println(Arrays.toString(array[i]));
        }
        System.out.println("排序后的数组");
        /*外层循环表示循环轮次*/
        for (int i = 0; i < array.length-1; i++) {
            //内层循环：每一趟循环都从数列的当前两个元素开始比较，比较到无序数组的最后
            for (int j = 0; j < array.length-1-i; j++) {
                /*判断当前元素的答题数量是否大于后面一位元素的答题数量*/
                if (Integer.valueOf(array[j][1]) > Integer.valueOf(array[j+1][1])){
                    /*大于则进行换位*/
                    String[] temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                    /*如果答题数量相等的话则判断答题时间*/
                }else if (Integer.valueOf(array[j][1]) == Integer.valueOf(array[j+1][1])){
                    if (Integer.valueOf(array[j][2]) > Integer.valueOf(array[j+1][2])){
                        String[] temp = array[j+1];
                        array[j+1] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }
        for (int i = 0; i < array.length; i++) {
            System.out.println(Arrays.toString(array[i]));
        }
    }
}
